<?php
include_once('Sismo.class.php');
 $data = file_get_contents('http://www.edreams.cl/terr/parseXml2.php');
 
function parseTable($html)
{

  // Find the table
  preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html);

  // Get title for each row
  preg_match_all("/<th.*?>(.*?)<\/[\s]*th>/", $table_html[0], $matches);
  $row_headers = $matches[1];

  // Iterate each row
  preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);

  
  $table = array();

  foreach($matches[1] as $row_html)
  {
    preg_match_all("/<td.*?>(.*?)<\/[\s]*td>/", $row_html, $td_matches);
	

    $row = array();
    for($i=0; $i<count($td_matches[1]); $i++)
    {
      $td = strip_tags(html_entity_decode($td_matches[1][$i]));
	  
      $row[$row_headers[$i]] = $td;

    }
    if(count($row) > 0)
      $table[] = $row;

  }
  
  return $table;
}

function parseTable2($html)
{
  // Find the table
  preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html);


 

  // Iterate each row
  preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);

   $row_headers = $matches[1];

$j=0;

  foreach($matches[1] as $row_html)
  {
    
	preg_match("~&q=(-?\d+\.?\d*),(-?\d+\.?\d*)~", $row_html, $td_matches);  
	  
	
       $row[$j] = $td_matches;
	   $j=$j+1;

	

    }
  
  return $row;

  
}





$output1 = parseTable2($data);
$output2 = parseTable($data);


unset($output1[0]);
$output1 = array_values($output1);
//print_r($output2);
	
$count = count($output1);


for ($i = 0; $i < $count; $i++) {

$grado=$output2[$i]['<div align="center"><span class="Estilo1">grado</span></div>'];
$fecha=$output2[$i]['<span class="Estilo1">fecha</span>'];
$ubicacion=$output2[$i]['<span class="Estilo1">epicentro</span>'];
$profundidad=$output2[$i]['<span class="Estilo1">profundidad</span>'];
$latitud=$output1[$i][1];
$longitud=$output1[$i][2];

$sismo = new Sismo("","1", $fecha, $grado, $longitud, $latitud, $profundidad,"Chile",$ubicacion,"SCHILE");    

$sismo->save();



	
	 }
echo "OK";

?>

